package org.grottarossa.ui.core.casemanagement;


import java.util.ArrayList;
import java.util.Hashtable;

import org.grottarossa.template.data.pojo.table.Record;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.template.drivers.components.tokens.CaseManagementId;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.base.IBaseComponent;
import org.grottarossa.ui.core.layout.EmptyPanel;
import org.grottarossa.ui.core.layout.LayoutFactory;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Layout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;

/***
 * Rappresenta il dettaglio di un Case Folder
 * @author 
 *
 */
public class CaseManagementPanel extends HorizontalSplitPanel
{
	private static final long serialVersionUID = -2786814442112409950L;
	private CaseManagementId caseFolderId;
	private IBaseComponent m_view = null;
	private Layout m_dispatchButtonsBar;
	private final String[] m_layouts;
	private final Hashtable<String, String> m_layoutsCaptions;
	private final ApplicationSession m_appSession;

	/***
	 * Costruttore
	 * @param id Id del case folder da rappresentare
	 * @param dispatchButtonsBar Barra dei bottoni sulla quale devono essere attachati eventuali pulsanti
	 * @param layouts Nomi dei layouts da caricare sulla sinistra
	 * @param layoutsCaptions Caption delle sezioni contenenti i layout sulla sinistra
	 * @param showButtonsBarForLayouts Elenco dei layout per cui deve essere mostrata la barra dei pulsanti
	 * @param parentView View sulla quale � attachato il componente
	 */
	public CaseManagementPanel(ApplicationSession appSession, CaseManagementId id, String [] layouts, Hashtable<String, String> layoutsCaptions,
			ArrayList<String> showButtonsBarForLayouts, IBaseComponent parentView) throws Exception
	{
		this.m_appSession = appSession;
		this.m_view = parentView;
		this.caseFolderId = id;
		this.m_layouts = layouts;
		this.m_layoutsCaptions = layoutsCaptions;
		this.initialize(true, showButtonsBarForLayouts);
	}

	/***
	 * Costruttore
	 * @param id Id del case folder da rappresentare
	 * @param dispatchButtonsBar Barra ei bottoni sulla quale devono essere attachati eventuali pulsanti
	 * @param layoutName Nome del layout da caricare sulla sinistra
	 * @param parentView View sulla quale � attachato il componente
	 */
	public CaseManagementPanel(ApplicationSession appSession, CaseManagementId id, HorizontalLayout dispatchButtonsBar,
			String layoutName, IBaseComponent parentView) throws Exception
	{
		m_appSession = appSession;
		this.caseFolderId = id;
		this.m_dispatchButtonsBar = dispatchButtonsBar;
		this.m_view = parentView;
		this.m_layouts = new String[] { layoutName };
		this.m_layoutsCaptions = new Hashtable<String, String>();

		ArrayList<String> showButtonsBarForLayouts = new ArrayList<String>();
		showButtonsBarForLayouts.add(layoutName);
		this.initialize(false, showButtonsBarForLayouts);
	}

	/***
	 * Inizializza il componente
	 * @param createButtonsBar se true gestisce autonomamente i pulsanti creando una barra
	 * @param showButtonsBarForLayouts
	 */
	private void initialize(boolean createButtonsBar, ArrayList<String> showButtonsBarForLayouts)
	throws Exception
	{
		VerticalLayout vl = null;
		TabSheet leftLayouts = new TabSheet();
		leftLayouts.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		leftLayouts.setHeight(100, Sizeable.UNITS_PERCENTAGE);

		for(String sLayout : m_layouts)
		{
			Component layoutComponent;
			vl = new VerticalLayout();
			vl.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			vl.setHeight(100, Sizeable.UNITS_PERCENTAGE);
			vl.setMargin(true);

			Layout buttonsBar = m_dispatchButtonsBar;

			if(createButtonsBar)
			{
				buttonsBar = new HorizontalLayout();
				vl.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			}

			if(!showButtonsBarForLayouts.contains(sLayout))
				buttonsBar = null;

			try
			{
				Record record = m_appSession.getBuilder().getRecordManagement().getCaseObject(caseFolderId);
				layoutComponent = (Component) m_appSession.getLayoutFactory().createLayout(
						sLayout, new Class[] { Record.class, Layout.class, CaseManagementId.class},  new Object[] { record, buttonsBar, caseFolderId} );
			}
			catch (DriverException e)
			{
				layoutComponent = new EmptyPanel(caseFolderId, (HorizontalLayout) m_dispatchButtonsBar, sLayout);
				e.printStackTrace();
			}

			layoutComponent.setWidth(100, Sizeable.UNITS_PERCENTAGE);
			layoutComponent.setHeight(100, Sizeable.UNITS_PERCENTAGE);
			vl.addComponent(layoutComponent);

			if(!createButtonsBar || showButtonsBarForLayouts.contains(sLayout))
			{
				vl.addComponent(buttonsBar);
				vl.setExpandRatio(layoutComponent, 1000);
				vl.setExpandRatio(buttonsBar, 1);
			}

			if(m_layouts.length > 1)
			{
				Tab t = leftLayouts.addTab(vl);
				String caption = (m_layoutsCaptions.containsKey(sLayout)) ? m_layoutsCaptions.get(sLayout) : sLayout;
				t.setCaption(caption);
			}
		}

		CaseTabSheet tabSheet = new CaseTabSheet(m_appSession, caseFolderId, m_view);
		tabSheet.setSizeFull();

		if(m_layouts.length > 1)
			this.setFirstComponent(leftLayouts);
		else
			this.setFirstComponent(vl);

		this.setSecondComponent(tabSheet);

		leftLayouts.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		tabSheet.setHeight(100, Sizeable.UNITS_PERCENTAGE);
	}
}
